package com.gj.controller;

import com.gj.model.TransactionMessage;
import com.gj.service.TransactionMessageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * author:guijie
 * date:${date}
 * description:TODO
 */
@RestController
@RequestMapping("/message")
public class TransactionMessageController {

    @Autowired
    private TransactionMessageService service;

    /**
     * 发送消息，将消息存入mysql中。具体发送消息逻辑由发送消息服务完成
     */
    @RequestMapping("/sendMessage")
    public boolean sendMessage(@RequestBody TransactionMessage message){
        return service.sendMessage(message);
    }

     /** @Author GuiJie
      * @Description //TODO 批量发送消息（暂未实现）
      * @Date 16:28 2020/10/15
      * @Param
      * @return
      */

     public boolean sendMessage(@RequestBody List<TransactionMessage> messages){
         return service.sendMessage(messages);
     }

      /** @Author GuiJie
       * @Description //TODO 确认消息被消费
       * @Date 16:59 2020/10/15
       * @Param [consumerSystem, id]   消费系统   id
       * @return boolean
       */
      @RequestMapping("/confirmCustomerMessage")
     public boolean confirmCustomerMessage(@RequestParam("consumerSystem") String consumerSystem,@RequestParam("id") Long id){
         return service.confirmCustomerMessage(consumerSystem,id);
     }

      /** @Author GuiJie
       * @Description //TODO 查询最早没有被消费的消息
       * @Date 17:01 2020/10/15
       * @Param
       * @return
       */
      @RequestMapping("/findByWatingMessage")
     public List<TransactionMessage> findByWatingMessage(){
        return service.findByWatingMessage();
     }

      
       /** @Author GuiJie
        * @Description //TODO 确认消息死亡
        * @Date 17:11 2020/10/15
        * @Param 
        * @return
        */
       @RequestMapping("/confirmDieMessage")
     public boolean confirmDieMessage(@RequestParam("id")Long id){
         return service.confirmDieMessage(id);
     }


       /** @Author GuiJie
        * @Description //TODO 累加发送次数
        * @Date 17:36 2020/10/15
        * @Param
        * @return
        */
       @RequestMapping("/incrSendCount")
     public boolean incrSendCount(@RequestParam("id") Long id,@RequestParam("sendDate") String sendDate) throws ParseException {
         //isBlank() null为true否在为false
         if (StringUtils.isBlank(sendDate)){
             return service.incrSendCount(id, new Date());
         }else {
             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             Date parse = format.parse(sendDate);
             return service.incrSendCount(id, parse);
         }
     }


       /** @Author GuiJie
        * @Description //TODO 重新发送当前已死亡的消息（暂未实现）
        * @Date 17:39 2020/10/15
        * @Param
        * @return
        */
     public boolean retrySendDieMessage(){
         return service.retrySendDieMessage();
     }


       /** @Author GuiJie
        * @Description //TODO 查询具体状态的消息
        * @Date 17:50 2020/10/15
        * @Param
        * @return
        */
       @RequestMapping("/findMessageByStatus")
     public List<TransactionMessage> findMessageByStatus(Integer status){
         return service.findMessageByStatus(status);
     }

}
